package com.yunxun.coinmessage.controller;

import com.yunxun.coinmessage.entity.Exchange;
import com.yunxun.coinmessage.templatedata.MsgResult;
import com.yunxun.coinmessage.service.ExchangeService;
import com.yunxun.coinmessage.service.MesSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.List;

@Controller
public class ExchangeController {
    /**
     * 交易所控制器
     */
    @Autowired
     ExchangeService exchangeService;
    @Autowired
    MesSourceService mesSourceService;

    /**
     * 添加交易所
     * @param exchange
     * @return
     */
    @RequestMapping("/addExchange")
    @ResponseBody
    public MsgResult addExchange(@RequestBody @Valid Exchange exchange, BindingResult result)throws Exception{
        if(result.hasErrors()){
            throw new Exception(result.getFieldError().getDefaultMessage());
        }
        if(exchange.getEname()==""){
            throw new Exception("交易所名称不能为空");
        }
         return   exchangeService.addExchange(exchange);
    }
    @RequestMapping("/queryExchangeById")
    public String queryById(Integer eid, Model model){
        Exchange exchange=exchangeService.queryById(eid);
        model.addAttribute("exchange",exchange);
        return "exchange";
    }
    @RequestMapping("/deleteExchangeById")
    @ResponseBody
    public  MsgResult deleteExchangeById(Integer eid){
            return exchangeService.deleteById(eid);

    }


    @RequestMapping("/queryExchangeByPage")
    public String queryByPage(@RequestParam("start")int start,@RequestParam("limit")int limit, Model model){
       List<Exchange> exchangeList= exchangeService.queryAll();
       Integer total=exchangeService.queryTotal();
       Integer totalPage=(total+19)/20;
       model.addAttribute("totalPage",totalPage);
       model.addAttribute("total",total);
       model.addAttribute("exchangeList",exchangeList);
       return "exchange-list";
    }
    @RequestMapping("/queryExchangeByPage1")
    @ResponseBody
    public List<Exchange> queryByPage1(@RequestParam("start")int start,@RequestParam("limit")int limit){
        List<Exchange> exchangeList= exchangeService.queryByPage(start,limit);
        return exchangeList;
    }
    /**
     * 仅仅是更新交易所
     * @param exchange
     *
     */
    @RequestMapping("/editExchangeById")
    @ResponseBody
    public MsgResult editExchange(Exchange exchange)throws Exception{
        if(exchange.getEname()==""){
            throw new Exception("交易所名称不能为空");
        }
         return  exchangeService.updateById(exchange);
    }

    //打开编辑的窗口并回显数据
    @RequestMapping("/openEditExchangeWindow")
    public String openEditWindow(Integer eid,Model model){
      Exchange exchange=exchangeService.queryById(eid);
      model.addAttribute("exchange",exchange);
       return "exchange-edit";
    }
    @RequestMapping("/openAddExchangeWindow")
    public String openAddExchangeWindow(){
        return "exchange-add";
    }

}
